package com.example.readfiletest;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Message;
import android.util.Log;
import android.widget.ArrayAdapter;

public class ReadTask extends AsyncTask<String,Integer,List> {

	private static final String TAG = ReadTask.class.getSimpleName();

	private ZichanActivity activity;
	
	public ReadTask(ZichanActivity activity) {
		this.activity = activity;
	}
	
	@Override
	protected void onPostExecute(List result) {
		Log.e(TAG,"size: "+result.size());
		this.activity.getPd().cancel();
		this.activity.setAdapter(new ZichanArrayAdapter(this.activity, R.layout.zichan_list, result));
		this.activity.getZichanLv().setAdapter(this.activity.getAdapter());
		this.activity.getAdapter().notifyDataSetChanged();
		super.onPostExecute(result);
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		// TODO Auto-generated method stub
		super.onProgressUpdate(values);
		if(this.activity.getPd() != null)
			this.activity.getPd().setProgress(values[0]);
		else
			Log.e(TAG,"pd is null");
	
	}

	@Override
	protected List doInBackground(String... params) {
		Log.e(TAG,"params[0]:"+params[0]);
		return searchByBuffered(params);
		//return searchByRandom(params);
	}
	
	
	private List searchByBuffered(String... params) {
		List retList = new ArrayList();
    	FileInputStream fis = null;
    	//BufferedReader br = null;
    	BufferedInputStream bis = null;
    	int pos = 0;
    	int oldProgress = 0;
		try {
			File file = new File(Environment.getExternalStorageDirectory().getPath()+File.separator+
					"pandian1.txt");
			fis = new FileInputStream(file);
			//br = new BufferedReader(new InputStreamReader(fis,Charset.forName("GBK")));
			bis = new BufferedInputStream(fis);
			String content = null;
			Long size = file.length();
			int line = (int) (size / ZichanConstants.LINE_CHARS);
			//while( (content = br.readLine()) != null) {
			for(int i = 0; i < line; i++) {
				//Product product = ProductWrap.getProduct(content, pos);
				Product product = ProductWrap.getProduct(bis, pos);
				Log.d(TAG,"pandian: "+product.getPandianflg());
				if(params[0].equals(product.getPandianflg())) {
					retList.add(product);
				}
				pos++;
				//Log.i(TAG,content);
				int progress = (pos* 100/line) ;
				
				
				if(oldProgress != progress) {
					oldProgress = progress;
					Log.e(TAG, "pos:"+ pos +" size: "+size + " line:" + line +" progress: "+progress);
					//pd.setProgress(progress);
					/*Message message = handler.obtainMessage(ZichanConstants.ZICHAN_RECORD_READING, progress, 0);
					message.sendToTarget();*/
					this.publishProgress(progress);
				}
			}
			
			
			
			/*Message message = handler.obtainMessage(ZichanConstants.ZICHAN_RECORD_READED, 0, 0);
			message.sendToTarget();*/
		} catch (FileNotFoundException e) {
			Log.e(TAG,e.getMessage());
		} catch (IOException e) {
			Log.e(TAG,e.getMessage());
		} finally {
			try {
				/*if(br != null) {
					br.close();
					br = null;
				}*/
				if(bis != null) {
					bis.close();
					bis = null;
				}
				if(fis != null) {
					fis.close();
					fis = null;
				}
			} catch (IOException e) {
				Log.e(TAG,e.getMessage());
			}
		}
    	
    	
    	return retList;
	}

	private List searchByRandom(String... params) {
		List retList = new ArrayList();
    	//FileInputStream fis = null;
    	//BufferedReader br = null;
    	//BufferedInputStream bis = null;
		RandomAccessFile random = null;
    	int pos = 0;
    	int oldProgress = 0;
		try {
			random = new RandomAccessFile(Environment.getExternalStorageDirectory().getPath()+File.separator+
					"pandian1.txt","rw");
			//br = new BufferedReader(new InputStreamReader(fis,Charset.forName("GBK")));
			//bis = new BufferedInputStream(fis);
			String content = null;
			Long size = random.length();
			int line = (int) (size / ZichanConstants.LINE_CHARS);
			//while( (content = br.readLine()) != null) {
			for(int i = 0; i < line; i++) {
				random.seek(ZichanConstants.LINE_CHARS*i);
				//Product product = ProductWrap.getProduct(content, pos);
				Product product = ProductWrap.getProduct(random, null, pos);
				Log.d(TAG,"pandian: "+product.getPandianflg());
				if(params[0].equals(product.getPandianflg())) {
					retList.add(product);
				}
				pos++;
				//Log.i(TAG,content);
				int progress = (pos* 100/line) ;
				
				
				if(oldProgress != progress) {
					oldProgress = progress;
					Log.e(TAG, "pos:"+ pos +" size: "+size + " line:" + line +" progress: "+progress);
					//pd.setProgress(progress);
					/*Message message = handler.obtainMessage(ZichanConstants.ZICHAN_RECORD_READING, progress, 0);
					message.sendToTarget();*/
					this.publishProgress(progress);
				}
			}
			
			
			
			/*Message message = handler.obtainMessage(ZichanConstants.ZICHAN_RECORD_READED, 0, 0);
			message.sendToTarget();*/
		} catch (FileNotFoundException e) {
			Log.e(TAG,e.getMessage());
		} catch (IOException e) {
			Log.e(TAG,e.getMessage());
		} finally {
			try {
				/*if(br != null) {
					br.close();
					br = null;
				}*/
				/*if(bis != null) {
					bis.close();
					bis = null;
				}
				if(fis != null) {
					fis.close();
					fis = null;
				}*/
				if(random != null) {
					random.close();
					random = null;
				}
			} catch (IOException e) {
				Log.e(TAG,e.getMessage());
			}
		}
    	
    	
    	return retList;
	}
}
